<template>
  <div class="layout-container history-container">
    <!-- 表单 -->
    <PageHeader title="历史样本数据管理">
      <template #btns>
        <div class="search common-buttons">
          <div class="span1">
            <span>历史样本名称</span>
          </div>
          <el-input v-model.trim="formtable.sampleName" autocomplete="off" placeholder="请输入" clearable/>
          <div class="span1">
            <span>历史数据名称</span>
          </div>
          <el-input v-model.trim="formtable.sampleDataName" autocomplete="off" placeholder="请输入" clearable/>
          <!-- <el-select 
          v-model.trim="formtable.sampleName" 
          :loading="historynameloading" 
          filterable 
          remote 
          :remote-method="gethistorynameOptions" 
          placeholder="请输入" clearable>
            <el-option 
              v-for="item in historynameOptions"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select> -->
          <el-button type="primary" :icon="Search" @click="onQuery({ pageNo: 1 })">搜索</el-button>
        </div>
      </template>
      <template #search>
      </template>
    </PageHeader>
    <!-- 表格 -->
    <div class="layout-container-table">
      <CommonTable
        :showSelection=false
        v-loading="loading" 
        v-model:page="page"
        :total="total"
        :data="state.tableData"
        @onQuery="onQuery"
        :header-cell-style="{'text-align':'center'}" 
        :cell-style="{'text-align':'center'}" 
        style="width: 100%">
        <el-table-column prop="sampleName" label="历史样本名称"  />
        <el-table-column prop="sampleDataName" label="历史数据名称"  />
        <el-table-column prop="time" label="时间"  />
        <el-table-column prop="sekname" label="导弹名称"  />
        <el-table-column prop="tgtname" label="目标名称"  />
        <el-table-column prop="jamname" label="干扰装备名称"  />
        <el-table-column label="查看历史样本"  >
          <template #default="scope">
            <el-button 
              link
              size="small" 
              @click="onCheck(scope.row.sampleName,scope.row.sampleDataName,scope.row.time,scope.row)" text>查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="评估结果文档"  >
          <template #default="scope">
            <el-button
              link
              size="small" 
              @click="onExport(scope.row.time,scope.row.sampleName,scope.row.sampleDataName)" text>文档下载</el-button>
          </template>
        </el-table-column>
      </CommonTable>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive } from "vue";
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { gethistoryData, downloadExcelFile } from '@/api/historySampleData'
import { useRoute, useRouter } from 'vue-router';
import PageHeader from '@/components/page-header/index.vue';
import CommonTable from '@/components/common-table/index.vue';
import {
  Plus,
  Delete,
  Download,
  Promotion,
  Search,
  Setting
} from '@element-plus/icons-vue';
import { Page } from '@/components/common-table/type';
import { downloadFile } from '@/utils/tools' 
let $router = useRouter();
const state = reactive<any>({
  tableData: [],
});
const page = reactive<Page>({
  current: 1,
  pageSize: 20,
});
const total = ref<number>(0);
const loading = ref(false);
//表单
const formtable = reactive({
  sampleName: "",
  sampleDataName: "",
});

onMounted(() => {
  onQuery({ pageNo: 1 });
});

//表单条件搜索
const onQuery= async(val:any) =>{
  let params = {
    pageNo:page.current,
    pageNum:page.pageSize,
    sampleName:formtable.sampleName?formtable.sampleName:null,
    sampleDataName:formtable.sampleDataName?formtable.sampleDataName:null,
  }
  loading.value = true;
  // let result = await gethistoryData(params);
  let result = {
        code: 200,
        totalNum: 3,
        data: {
          dataarray:
            [{
              time: 1,
              sampleName: "试验样本一",
              sampleDataName: "历史数据一",
              sekid: 1,
              sekname: "导弹01",
              seklon: 1,
              seklat: 1,
              sekhei: 1,
              sekvel: 1,
              tgtid: 1,
              tgtname: "目标01",
              tgtlon: null,
              tgtlat: 1,
              tgthei: null,
              tgtvel: 1,
              jamid: 1,
              jamname: "干扰01",
              jamparentid: 1,
              jamparentname: 1,
              jammaxrange: 1,
              jamcurrange: 1,
              jamStyle: 1,
              jamActionStyle: 1,
            },
            {
              time: 1,
              sampleName: "试验样本一",
              sampleDataName: "历史数据二",
              sekid: 1,
              sekname: "导弹01",
              seklon: 1,
              seklat: 1,
              sekhei: 1,
              sekvel: 1,
              tgtid: 1,
              tgtname: "目标01",
              tgtlon: 1,
              tgtlat: 1,
              tgthei: 1,
              tgtvel: 1,
              jamid: 1,
              jamname: "干扰01",
              jamparentid: 1,
              jamparentname: 1,
              jammaxrange: 1,
              jamcurrange: 1,
              jamStyle: 1,
              jamActionStyle: 1,
            },
            {
              time: 1,
              sampleName: "试验样本二",
              sampleDataName: "历史数据一",
              sekid: 1,
              sekname: "导弹01",
              seklon: 1,
              seklat: 1,
              sekhei: 1,
              sekvel: 1,
              tgtid: 1,
              tgtname: "目标01",
              tgtlon: 1,
              tgtlat: 1,
              tgthei: 1,
              tgtvel: 1,
              jamid: 1,
              jamname: "干扰01",
              jamparentid: 1,
              jamparentname: 1,
              jammaxrange: 1,
              jamcurrange: 1,
              jamStyle: 1,
              jamActionStyle: 1,
            },]
        },
        msg: ''
      };
  loading.value = false;
  //渲染视图
  total.value=result.totalNum;
  state.tableData=result.data.dataarray;
};
//加密
const encode = (str:string) =>{
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
    function toSolidBytes(match, p1) {
        return String.fromCharCode('0x' + p1);
  }));
}
//需要加密
const onCheck = (historyname:any,hisdataname:any,time:any,row:any) => {
  let rowvalue = encode(JSON.stringify(row));
  $router.push({
    path: '/testSampleDataManageSystem/historySampleData/checkinfo',
    query: {samplename: historyname,sampledataname: hisdataname, sampletime: time,value: rowvalue},
  });
};
// 导出
const onExport = async(timeval:any,name:any,dataname:any) => {
  await downloadFile(downloadExcelFile,{ "data": {"sampleName":name,"sampleDataName":dataname,"timer":timeval} },'历史试验样本-'+name+'-历史试验数据-'+dataname+'-时间-'+timeval);
}
// const downloadFile = async(id: any, fileName: string) => {
//   let params = {
//     id:id,
//   }
//   await downloadExcelFile(params).then((res: any) => {
//     let downloadElement = document.createElement('a');
//     // 后台返回res是blob类型，直接使用res是arraybuffer,res.data是undefined
//     let blob = new Blob([res], {type: 'application/vnd.ms-excel'});
//     // let _filename = res.headers['content-disposition'].split(';')[1].split('=')[1];
//     downloadElement.style.display = 'none';//隐藏

//     const url = window.URL || window.webkitURL;
//     downloadElement.href = url.createObjectURL(blob);
//     downloadElement.download = fileName+ '.xls';
//     document.body.appendChild(downloadElement);
//     downloadElement.click(); // 点击下载
//     document.body.removeChild(downloadElement);
//     url.revokeObjectURL(downloadElement.href); // 释放掉blob对象
//   })
//   .catch((e)=>{
//     console.log(e);
//   })
// };
</script>
<style  lang="scss" scoped>
@import '@/assets/style/buttons';
.history-container {
  .search {
    // align-items: center;
    display: flex;
    // line-height: 100%;
    // margin:auto;
    :deep() {
      .el-input--small .el-input__wrapper {
        margin-right: 10px;
      }
    }
  }
  .layout-container-table {
    .el-button--small {
      background: transparent;
      color: rgba(24, 81, 237, 1);
      font-size: 13px;
    }
  }
  .search .span1{
    display: inline-block;
    width: 100px;
    margin: auto;
    font-size:14px;
    text-align: center;
  }
  .search .el-input{
    width: 200px;
    margin: auto;
  }
  :deep() {
    .el-dialog__body {
      padding: 10px 20px;
    }
  }
}
</style>